package common.security;

import java.util.Collection;
import java.util.Date;

import org.springframework.security.core.GrantedAuthority;

public class User extends org.springframework.security.core.userdetails.User {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8296467401592517580L;

	private static boolean isNonExpired(common.rbac.po.User users) {
		boolean accountNonExpired = true;
		if (null != users.getExpiredDt()) {
			Date date = new Date();
			accountNonExpired = date.before(users.getExpiredDt());
		}
		return accountNonExpired;
	}

	private common.rbac.po.User user;
	private boolean isAdmin;

	public User(common.rbac.po.User users, Collection<? extends GrantedAuthority> authorities) {
		super(users.getName(), users.getPassword(), users.isEnabled(), isNonExpired(users), true, users.isNonLock(),
				authorities);
		this.user = users;
		for (GrantedAuthority grantedAuthority : authorities) {
			if ((Constant.ROLE_PREFIX + "ADMIN").equals(grantedAuthority.getAuthority())) {
				this.isAdmin = true;
				break;
			}
		}
	}

	public String getId() {
		return user.getId();
	}

	public String getPhone() {
		return user.getPhone();
	}

	public String getPicture() {
		return user.getPicture();
	}

	public boolean isAdmin() {
		return isAdmin;
	}

}
